#include "dlg_selectdid.h"
#include "ui_dlg_selectdid.h"
#include <QTextCodec>

Dlg_SelectDid::Dlg_SelectDid(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg_SelectDid)
{
    ui->setupUi(this);

    this->setWindowFlags(this->windowFlags()| Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
    ui->tableView->setModel(model_645_did);//sql模型与tableView绑定    
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//初始禁止编辑
    ui->tableView->resizeColumnsToContents();//列宽适应内容
    ui->tableView->setSortingEnabled(true);//排序使能
    ui->tableView->hideColumn(0);//隐藏主键列
    ui->tableView->setColumnWidth(1,200);


    for(uint row=0;row<sizeof(radio_find)/sizeof(radio_find[0]);row++)
        for(uint col=0;col<sizeof(radio_find[0])/sizeof(radio_find[0][0]);col++)
        {
            radio_find[row][col] = new Radio_my(ui->groupBox);
            radio_find[row][col]->setText("默认");
            radio_find[row][col]->setGeometry(QRect(20+col*60, 45 + 23*row, 61, 16));
            QObject::connect(radio_find[row][col],SIGNAL(clicked()),
                             this,SLOT(on_radio_find_clicked()));
        }

    ini_save_or_restore(false);
}

Dlg_SelectDid::~Dlg_SelectDid()
{
    ini_save_or_restore(true);
    delete ui;
}

void Dlg_SelectDid::on_buttonBox_accepted()
{
    int row = ui->tableView->currentIndex().row();
    QString did_str = model_645_did->record(row).value("DID").toString();//row行，DID列
    emit send_select(did_str);
}

void Dlg_SelectDid::on_checkBoxEditMode_clicked(bool checked)
{
    if(true == checked)
    {
        ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->pushButtonAddRow->setEnabled(true);
        ui->pushButtonDelRow->setEnabled(true);
        ui->pushButtonSave->setEnabled(true);
    }
    else
    {
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        ui->pushButtonAddRow->setEnabled(false);
        ui->pushButtonDelRow->setEnabled(false);
        ui->pushButtonSave->setEnabled(false);
    }

}

void Dlg_SelectDid::on_tableView_doubleClicked(const QModelIndex &index)
{
    qDebug()<<"双击的行号="<<index.row();
    if(QAbstractItemView::NoEditTriggers == ui->tableView->editTriggers())
    {
        qDebug()<<"选择了did并返回";
        Dlg_SelectDid::on_buttonBox_accepted();
        QDialog::accept();
    }
    else
    {
        qDebug()<<"可编辑单元格";
    }
}

void Dlg_SelectDid::on_pushButtonAddRow_clicked()
{
    int rowNum = model_645_did->rowCount();
    model_645_did->insertRows(rowNum, 1);//在最后一行插入1行空数据
    model_645_did->setData(model_645_did->index(rowNum,1),"");//至少要在非主键单元格中执行一次写入操作，否则存在空行，会提交失败
    if(model_645_did->submitAll())
    {
        qDebug()<<"添加行成功";
    }
    else
    {
        qDebug()<<"添加行失败:"<<model_645_did->lastError().text();
    }

}

void Dlg_SelectDid::on_pushButtonDelRow_clicked()
{
    //QMessageBox::information(this, tr("正在删除"), "正在删除");
    model_645_did->removeRow(ui->tableView->currentIndex().row());
    model_645_did->submitAll();
    model_645_did->setFilter("");//相当于刷新tableView的显示
}

void Dlg_SelectDid::on_pushButtonSave_clicked()
{
    //QMessageBox::information(this, tr("保存"), "保存");

    if(model_645_did->submitAll())
    {
        qDebug()<<"保存成功";
    }
    else
    {
        qDebug()<<"保存失败:"<<model_645_did->lastError().text();
    }

}
void Dlg_SelectDid::Filter_show(QString SearchStr)
{

    model_645_did->SetAllFilter(SearchStr);
    model_645_did->select();
}


void Dlg_SelectDid::on_pushButtonShowAll_clicked()
{
    ui->lineEditSearch->setText("");
    model_645_did->setFilter("");//全显    

}


void Dlg_SelectDid::on_lineEditSearch_cursorPositionChanged(int arg1, int arg2)
{
    arg1 = arg2 + 1;//避免警告
    arg2 =  arg1+ 1;//避免警告

    QString SearchStr = ui->lineEditSearch->text();
    Filter_show(SearchStr);


}



void Dlg_SelectDid::on_radio_find_clicked()
{
    Radio_my **radio_tmp = &(radio_find[0][0]);
//    qDebug("二维radio共有%d个元素", sizeof(radio_find)/sizeof(radio_find[0][0]));
//    qDebug("[0][0] addr = %x", &(radio_find[0][0]));
//    qDebug("radio_tmp = %x", radio_tmp);

    for(uint idx=0;idx<sizeof(radio_find)/sizeof(radio_find[0][0]);idx++)
    {
        if((*radio_tmp)->isChecked())
        {
            Filter_show((*radio_tmp)->text());
            return;
        }
        (radio_tmp)++;
    }
}
void Dlg_SelectDid::ini_save_or_restore(bool isSave)
{
    QSettings *IniFile = new QSettings("meter.ini", QSettings::IniFormat);
    IniFile->setIniCodec("UTF-8"); ;
    if(isSave == true)
    {

        QString radio_all_str="";
        Radio_my **radio_tmp = &(radio_find[0][0]);
        for(uint idx=0;idx<sizeof(radio_find)/sizeof(radio_find[0][0]);idx++)
        {
            radio_all_str += (*radio_tmp)->text() + " ";
            radio_tmp++;
        }

        IniFile->setValue("radio_find/did_645", radio_all_str);

        qDebug()<<"645 did 配置信息已保存:"<<radio_all_str;
    }
    else
    {


        QString radio_all_str = IniFile->value("radio_find/did_645",(QVariant)"").toString();
        QStringList radio_str_L = radio_all_str.split(" ");
        qDebug("645 did 配置信息共有%d条",radio_str_L.count());

        Radio_my **radio_tmp = &(radio_find[0][0]);
        for(int idx=0;idx<(int)(sizeof(radio_find)/sizeof(radio_find[0][0]));idx++)
        {

            if(idx<radio_str_L.count())
                (*radio_tmp)->setText( radio_str_L.at(idx));
            else
                (*radio_tmp)->setText( "电能");
            radio_tmp++;
        }




        qDebug()<<"645 did 配置信息已读出";
    }

    delete IniFile;

}

